<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">曲师商城</view>
			<view>
				<u--form labelPosition="left" :model="user" :rules="rules" ref="userForm">
					<u-form-item @click="inputChange" label="昵称" prop="nickName" borderBottom ref="nickName">
						<u--input v-model="user.nickName" border="none"></u--input>
					</u-form-item>
					<u-form-item @click="inputChange" label="密码" prop="password" borderBottom ref="password">
						<u--input type="password" v-model="user.password" border="none"></u--input>
					</u-form-item>
				</u--form>
			</view>
			<button @click="doPwdLogin" :style="{'background-color': bgcolor,'color': fontcolor,'margin-top': top}"
				class="getCaptcha">密码登录</button>
		</view>
		<view class="buttom">
			<view class="loginType">
				<view class="wechat item">
					<view class="icon"><u-icon size="40" name="weixin-fill" color="rgb(83,194,64)"></u-icon></view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon"><u-icon size="40" name="qq-fill" color="rgb(17,183,233)"></u-icon></view>
					QQ
				</view>
			</view>
			<view class="hint">
				登录代表同意
				<text class="link">用户协议、隐私政策，</text>
				并授权使用您的账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		pwdLogin
	} from "@/api/login/index.js";

	const user = ref({
		nickName: '',
		password: ''
	})

	//表单校验
	const userForm = ref();
	const rules = ref({
		nickName: [{
			required: true,
			message: '请输入用户昵称',
			trigger: ['blur', 'change'],
		}, ],
		password: [{
			required: true,
			message: '请输入密码',
			trigger: ['blur', 'change'],
		}, ],
	})

	//密码登录方法
	const doPwdLogin = () => {
		userForm.value.validate().then((valid) => {
			//开始登录
			pwdLogin(user.value).then((res) => {
				if (res.code === 200) {
					console.log(res.data);
					//本地存储用户token
					uni.setStorageSync("token", res.data.authorization);
					//本地存储用户信息
					uni.setStorageSync("user", res.data.user);
					uni.showToast({
						title: "登录成功！"
					});
					setTimeout(() => {
						uni.switchTab({
							url: "/pages/index/index"
						})
					}, 1000)
				}
			})
		}).catch(err => {
			console.log(err, '校验失败');
		})
	}


	const bgcolor = ref('#fcbd71');
	const fontcolor = ref('#82848a');
	const top = ref("50px");
	const inputChange = () => {
		if (user.value.nickName !== '') {
			bgcolor.value = '#f9ae3d';
			fontcolor.value = '#fff';
		} else {
			bgcolor.value = '#fcbd71';
			fontcolor.value = '#82848a';
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
				font-size: 12px;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30px;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-warning;
				}
			}
		}
	}
</style>